<html>
<head>
<style type="text/css">
    
 .radio {
  background:#C9C9C9 !important;
  border:none !important;	
  color: #fff !important;  	
}

.ui-button.ui-state-active .ui-button-text {
  background-image: linear-gradient(to bottom, #8aaaff, #006db0);
  color: #fff; 
  border-radius:5px; 	
}    
    
</style>
	<script type="text/javascript">
		var aJaxURL			= "server-side/letters/letterB.action.php";						//server side folder url
		var seoyURL			= "server-side/seoy/seoy.action.php";							//server side folder url
        var wsdlURL			= "server-side/wsdl/wsdl.action.php";							//server side folder url
		var tName			= "example";													//table name
		var fName			= "add-edit-form";												//form name
		var trans_type      =  0; 
		var trans_price     =  0;

		var tbName			= "tabs";	
		var filter          =  'AND a_letter.b_letter_status=0'  ;

		$(document).ready(function () {	
			LoadTable(tName);
			/* Add Button ID, Delete Button ID */
			SetEvents("", "", "", tName, fName, aJaxURL);
			gsUnity.GetTabs("tabs");

			$("#print_empty").button({
	            icons: {
	                primary: "ui-icon-print"
	            }
        	});

		});

		function LoadTable(table){
			 filter          =  'AND a_letter.b_letter_status=0'  ;
			var tab = GetSelectedTab("tabs");
			if(tab==1) filter  = 'AND a_letter.b_letter_status!=0';
			switch (table) {
				default:
					/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
	                GetDataTable(tName, aJaxURL, "get_list", 7, "filter="+filter, 0, "", 1, "desc");
			}
		}

		function LoadDialog(){
			var buttons = {
			        "print-letterB": {
			            text: "ბეჭდვა",
			            id: "print-letterB",
			            click: function () {

			            	var link = GetRootDIR();
		        	    	link = link + "server-side/print/print_letterB.php?id="+$("#a_letter_id").val();

		        	    	var newWin = window.open(link, "JSSite", "width=800,height=800,resizable=yes,scrollbars=yes,status=yes");
		        	    	newWin.focus();
		        	    	newWin.onload = function() {
		        	    		newWin.print();
		        	    	}
			            }
			        },

			        "continue-rs": {
			            text: "rs.ge - ზე დასრულება",
			            id: "continue-rs",
			            click: function () {
			            }
			        },
			        "add-rs": {
			            text: "rs.ge - ზე გააქტიურება",
			            id: "add-rs",
			            click: function () {
			            }
			        },
			        "ref-rs": {
			            text: "rs.ge - ზე  გაუქმება",
			            id: "ref-rs",
			            click: function () {
			            }
			        },
			        "cancel": {
			            text: "დახურვა",
			            id: "cancel-dialog",
			            click: function () {
			                $(this).dialog("close");
			            }
			        }
			};
			var overhead_id	= $("#overhead_id").val();

			GetDialog(fName, 610, "auto", buttons);
			//alert($("#rs_status").val());
			if( $("#rs_end_date").val()!='') {
				$("#continue-rs").button("disable");
				$("#add-rs").button("disable");
				$("#ref-rs").button("disable");
			}
			if(empty(overhead_id) || overhead_id=='0'){
				$("#add-rs").button("enable");
				$("#continue-rs").button("disable");
				$("#ref-rs").button("disable");

			}else{
				$("#add-rs").button("disable");

				}
			 $("#add_toner").button().click(function() {
		if($("#toneri").val()>2){
	        	param = new Object();
	            //Action
	        	param.act	= "add_toner";
	        	param.wd_id	= $("#toneri").val();
			    $.ajax({
			        url: aJaxURL,
				    data: param,
			        success: function(data) {
						if(typeof(data.error) != "undefined"){
							if(data.error != ""){
								alert(data.error);
							}else{
								CloseDialog(fName);
							}
						}
				    }
			    });
			 }
		 else{alert("აირჩიეთ შტრიხკოდი");}
		 ;}
			 );;

			 $("#add_toner").button("disable")

		}

        $(document).on("click", ".new_requester", function (event) {
	    	if($(this).is(":checked")){
	    		$("#new_requester").css("display", "block");
	    		$("#requester").css("display", "none");
		    }else{
		    	$("#new_requester").val('');
	    		$("#new_requester").css("display", "none");
	    		$("#requester").css("display", "block");
			}
		});

        $(document).on("click", "#ok_part", function (event) {
        	param = new Object();

            //Action
        	param.act	= "save_bletter_status";
        	param.rp	= $("#receiver1").val();
    	    param.ci	= $("#car_id").val();
    	    param.id	= $("#a_letter_id").val();
    	    param.req	= $("#requester").val();
    	    param.nreq	= $("#new_requester").val();
    	    param.cobj	= $("#client_object_id").val();
		    $.ajax({
		        url: aJaxURL,
			    data: param,
		        success: function(data) {
					if(typeof(data.error) != "undefined"){
						if(data.error != ""){
							alert(data.error);
						}else{
							LoadTable();
						}
					}
			    }
		    });
		});

        // Add - RS
	    $(document).on("click", "#add-rs", function () {
	    	$("#client_ob_trans_div").remove();
	    	client_ob_trans=$("#client_ob_trans").val();
		    if(client_ob_trans>0){
		    	$.getJSON( "server-side/letters/empty_latter.action.php?act=trans_type", function( data ) {
		    		 $("body").append("<div id='client_ob_trans_div' title='ტრანსპორტირების ღირებულება'> შემოიტანეთ  ტრანსპორტირების  ღირებულება<input id='client_ob_trans_price' class='inpt' value='7.00' step='0.01' type='number'><br>აირჩიეთ ტრანსპორტირების ტიპი<br><select class='inpt' id='client_ob_trans_type'>"+data.data+"</select></div>");
	    	    
		    var button = {
		            "save": {
		                text: "გაგზავნა",
		                id: "",
		                click: function () {
		        		    var letter_id	= $("#latter_id").val();
		        		    trans_price=$("#client_ob_trans_price").val();
		        		    trans_type =$("#client_ob_trans_type").val()
		    			    var letter_id	= $("#a_letter_id").val();
		    			    var check = SendRS(letter_id);
		    		    	if(check){
		    	                alert("ზედნადები წარმატებით გაიგზავნა rs.ge-ზე!!!");
		    	                $("#add-rs").button("disable");
		    	                $("#continue-rs").button("disable");
		    		    	}
		        		    $("#client_ob_trans_div").remove();		        		    
		        		    //trans_type =0;
	
		                }
		            },
		            "cancel": {
		                text: "დახურვა",
		                id: "cancel-dialog",
		                click: function () {
		                	$("#client_ob_trans_div").remove();
		                    $(this).dialog("close");
		                }
		            }
		        };

		    GetDialog('client_ob_trans_div', 300, 250, button);
		    	})}
		    else{
			    var letter_id	= $("#a_letter_id").val();
			    var check = SendRS(letter_id);
		    	if(check){
	                alert("ზედნადები წარმატებით გაიგზავნა rs.ge-ზე!!!");
	                $("#add-rs").button("disable");
	                $("#continue-rs").button("disabled");

		    	}
		    }
		});

	    // Confit - RS
	    $(document).on("click", "#continue-rs", function () {
		    var letter_id	= $("#a_letter_id").val();
            $.ajax({
                url: aJaxURL,
                data: "act=get_overhead_id&id=" + letter_id,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
		    				var check = FinishOverheadRS(data.overhead_id);
		    				if (check) {
		                        alert("ზედნადები წარმატებით დასრულდა rs.ge-ზე!!!");
		                        MakeSale(letter_id);
		                        $("#continue-rs").button("disable");
		                        $("#add-rs").button("disable");
		                    }
                        }
                    }
                }
            });
		});

        function SendRS(letter_id) {
            var check = false;
            $.ajax({
                url: wsdlURL,
                async: false, //r-value
                data: "act=send_bletter&id=" + letter_id + "&trans_type=" + trans_type + "&price=" + trans_price,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            check = true;
                        }
                    }
                }
            });
            return check;
            trans_price=0;
            trans_type=0;
        }

        function FinishOverheadRS(rs_id) {
            var check = false;
            $.ajax({
                url: wsdlURL,
                async: false, //r-value
                data: "act=finish_overhead&rs_id=" + rs_id,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            check = true;
                        }
                    }
                }
            });
            return check;
        }

	    $(document).on("click", "#ref-rs", function () {
		    				var check = RefOverheadRS($("#overhead_id").val());
		    				if (check) {
		                        alert("ზედნადები წარმატებით გაუქმდა rs.ge-ზე!!!");
		                    }




		});

        function MakeSale(letter_id){
        	param = new Object();

            //Action
        	param.act	= "make_sale";
    	    param.id	= letter_id;

		    $.ajax({
		        url: aJaxURL,
			    data: param,
		        success: function(data) {
					if(typeof(data.error) != "undefined"){
						if(data.error != ""){
							alert(data.error);
						}else{
							LoadTable();
						}
					}
			    }
		    });
        }


	    $(document).on("click", "#print_empty", function () {


	    	var link = $(this).attr("str");
	    	link = "server-side/print/print_EmptyLetterB.php";

	    	var newWin = window.open(link, "JSSite", "width=800,height=800,resizable=yes,scrollbars=yes,status=yes");
	    	newWin.focus();
	    	newWin.onload = function() {
	    		newWin.print();
		    }
	    });
        function RefOverheadRS(o_id) {
            var check = false;
            $.ajax({
                url: wsdlURL,
                async: false, //r-value
                data: "act=RefOverheadRS&rs_id=" + o_id,
                success: function(data) {
                    if (typeof(data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            check = true;
                        }
                    }
                }
            });
            return check;
        }


    </script>
</head>

<body>
    <div id="tabs" style="width: 1140px; margin: 0 auto; min-height: 0px;">
		<ul>
			<li><a href="#tab-0">ხელმოსაწერი</a></li>		
			<li><a href="#tab-1">ხელმოწერილი</a></li>			
		</ul>
		<div id="tab-0"></div>
	<div id="tab-1"></div>
    <div id="dt_example" class="ex_highlight_row">
        <div id="container">
            <div id="dynamic">
            	<h2 align="center">ბ ბარათები</h2>
            	<div id="button_area">
            		<button id="print_empty">ცარიელი ბ ბარათის ბეჭდვა</button>        			
        		</div>
                <table class="display" id="example">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th class="min" title="შეფუთვის თარიღი">შეფუთვის თარიღი</th>
                            <th class="min">ბ ბარათი</th>
                            <th style="width: 100%">ობიექტი</th>
                            <th class="min">ღირებულება</th>
                            <th class="min">ხელმოწერილი</th>
                            <th class="min">ანგ. ფაქტურა</th>                            
                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="order_date" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="order_object" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="orderer" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="status" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="status" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="status" value="ფილტრი" class="search_init" />
                            </th>
                        </tr>
                    </thead>
                </table>
            </div>
            <div class="spacer">
            </div>
        </div>
    </div>
</div>
    <!-- jQuery Dialog -->
    <div id="add-edit-form" class="form-dialog" title="ბ ბარათი">
    	<!-- aJax -->
	</div>
</body>
</html>